package Redis.StringRedisTemplate;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import Redis.StringRedisTemplate.utils.RedisOperator;

@Component
public class RedisOperation implements ApplicationRunner {
	
	@Autowired
	RedisOperator redisOperator;
	
	@Autowired
	RedisUtil redisUtil;
	

	@Override
	public void run(ApplicationArguments args) throws Exception {
		// TODO Auto-generated method stub
		
		System.out.println("测试set , get 操作 \n");
		
		redisOperator.set("stringRedisTemplate", "stringRedisTemplate");
		
		redisOperator.lpush("stringRedisTemplateList", "value1");
		
		

		System.out.println("测试队列的 lpush , lpop , llen 等 操作 \n");
		
		System.out.println("key stringRedisTemplate 的value为："+redisOperator.get("stringRedisTemplate"));
		
		System.out.println("stringRedisTemplateList队列长度："+redisOperator.llen("stringRedisTemplateList"));
		


		System.out.println("测试过期设置 操作 \n");
		
		System.out.println("设置stringRedisTemplate 过期时间3s ");
		
		redisOperator.expire("stringRedisTemplate",2);
		
		System.out.println("休眠两秒。。");
		
		TimeUnit.SECONDS.sleep(2);
		
		System.out.println("检测key  stringRedisTemplate 是否过期："+redisOperator.ttl("stringRedisTemplate")+"( -2 代表 key过期已不存在)");
		
		System.out.println("•The command returns -2 if the key does not exist.\r\n" + 
				"•The command returns -1 if the key exists but has no associated expire.\r\n" + 
				"");
		
		System.out.println("其他命令测试：");
		redisOperator.testStringRedisTemplate();
		
		
		System.out.println("乐观锁测试############################################");
		
		redisOperator.set("OptismLock","task0");
		
		ExecutorService executor = Executors.newFixedThreadPool(3);
		
		// 开启事务支持，在同一个 Connection 中执行命令
		redisUtil.setEnableTransactionSupport(true);
		executor.submit(redisUtil.getTask("task1"));
		executor.submit(redisUtil.getTask("task2"));
		executor.submit(redisUtil.getTask("task3"));
		
		while(redisUtil.num.get()<3)
		{
			System.out.println("乐观锁测试中，再等待1s");
			TimeUnit.SECONDS.sleep(1);
		}
		

		System.out.println("乐观锁测试过后key的结果："+redisOperator.get("OptismLock"));
		
		System.out.println("测试redis事务回滚############################################");

		redisOperator.set("transaction", "1");
		redisOperator.set("transaction2", "1");
		
		System.out.println("【执行前】事务key transaction:"+redisOperator.get("transaction"));
		System.out.println("【执行前】事务key transaction2:"+redisOperator.get("transaction2"));
		
		
		redisUtil.testTransaction();
		

		System.out.println("【执行后】事务key transaction:"+redisOperator.get("transaction"));
		System.out.println("【执行后】事务key transaction2:"+redisOperator.get("transaction2"));
		

		System.out.println("redis事务测试结果：transaction的value值没变 ，自增操作回滚了");
		
	}

}
